capture log close
log using suic-econ-a7-sens.txt, replace text

//  program:    suic-econ-a7-sens.do
//  task:		sensitivity analyses for recessions on suicide
// 	input:		suic-econ-data1-census.dta
//	output:		suic-econ-table2.txt
//  project:    economic conditions and suicide
//  author:     sam harper \ 17dec2014

//  #0
//  program setup

version 12
set linesize 80
clear all
macro drop _all
set matsize 11000


// #1
// data and descriptive table

use suic-econ-data1-census, clear

* re-scale icei for a 10-unit change, change sign to estimate decrease
replace icei= -1*icei/10



// #2
// basic FE model

poisson suic icei i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_1: estpost margins, dydx(icei) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 
	


// #3
// lagged icei

* generate 6 and 12 month lagged values of ICEI
sort state sex race2 agecat quarter month
bysort state sex race2 agecat: gen iceil6=icei[_n-6]
bysort state sex race2 agecat: gen iceil12=icei[_n-12]

label var iceil6 "ICEI lagged 6 months"
label var iceil12 "ICEI lagged 12 months"

* lagged regression model: 6 months
poisson suic iceil6 i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_2: estpost margins, dydx(iceil6) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 	

* lagged regression model: 12 months
poisson suic iceil12 i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_3: estpost margins, dydx(iceil12) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 
	
	

// #4
// negative binomial model

nbreg suic icei i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_4: estpost margins, dydx(icei) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 
	

// #5 	
// heterogeneity by national recessionary period

* NBER recession periods
gen recper=0
* Jan1980-Jul1980
replace recper=1 if year==1980 & (month>=1 & month<=6)
* Jul1981-Nov1982
replace recper=1 if (year==1981 & month>=7 & month<=12 ) | ///
	(year==1982 & month>=1 & month<=10)
*Jul1990-Mar1991
replace recper=1 if (year==1990 & month>=7 & month<=12 ) | ///
	(year==1991 & month>=1 & month<=3)
*Mar2001-Nov2001
replace recper=1 if (year==2001 & month>=3 & month<=10 )
* Dec2007-Jun2009
replace recper=1 if (year==2007 & month==12 ) | ///
	(year==2009 & month>=1 & month<=5)

label var recper "NBER recession period?"
label define noyes 0 no 1 yes, modify
label values recper noyes

* check coding of national recessions by quarter
tab quarter recper

* average change in ICEI by recessionary period
sort stfipsn agecat sex race2 yyyymm
bysort stfipsn agecat sex race2: gen iceidiff=icei-icei[_n-1]
bysort stfipsn agecat sex race2: gen iceipdiff=(icei-icei[_n-1]) / icei[_n-1]
tab strec, sum(iceidiff)
tab recper, sum(iceidiff)
tab strec, sum(iceipdiff)
tab recper, sum(iceipdiff)

poisson suic c.icei##i.recper i.sex i.race2 i.agecat i.stabb ///
	i.season##i.year, offset(logpop100y) vce(cluster stabb) nolog ///
	irr cformat(%4.3f)

* predicted margins
eststo m_5: estpost margins, dydx(icei) by(recper) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 
	
* save estimates for plot
qui parmest, saving(recper, replace) 

	

// #6
// heterogeneity by state recessionary periods

poisson suic c.icei##i.strec i.sex i.race2 i.agecat i.stabb ///
	i.season##i.year, offset(logpop100y) vce(cluster stabb) nolog ///
	irr cformat(%4.3f)

* predicted margins
eststo m_5: estpost margins, dydx(icei) at(strec=(0 1)) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 

* save estimates for plot
qui parmest, saving(strec, replace) 

	

// #6
// using unemployment rather than ICEI (similar exposure contrast)

poisson suic unemprtsa i.sex i.race2 i.agecat i.stabb ///
	i.season##i.year, offset(logpop100y) vce(cluster stabb) ///
	nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_7: estpost margins, at(unemprtsa=(5.8 9.6)) predict(ir) ///
	contrast(atcontrast(r._at)) vce(unconditional) cformat(%4.2f)
	
* generate 6 and 12 month lagged values of unemployment rate
sort state sex race2 agecat quarter month
bysort state sex race2 agecat: gen url6=unemprtsa[_n-6]
bysort state sex race2 agecat: gen url12=unemprtsa[_n-12]

label var url6 "unemployment rate lagged 6 months"
label var url12 "unemployment rate lagged 12 months"
	
* lagged regression model: 6 months
poisson suic url6 i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_8: estpost margins, at(url6=(5.8 9.6)) predict(ir) ///
	contrast(atcontrast(r._at)) vce(unconditional) cformat(%4.2f) 	

* lagged regression model: 12 months
poisson suic url12 i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_9: estpost margins, at(url12=(5.8 9.6)) predict(ir) ///
	contrast(atcontrast(r._at)) vce(unconditional) cformat(%4.2f) 


// #7
// using cancer rather than suicide (similar exposure contrast)
	
poisson cncr icei i.sex i.race2 i.agecat i.stabb i.season##i.year, ///
	offset(logpop100y) vce(cluster stabb) nolog irr cformat(%4.3f)

* predicted marginal effects
eststo m_10: estpost margins, dydx(icei) predict(ir) ///
	vce(unconditional) cformat(%4.2f) 
	

	
// #8
// write results to file

esttab m_* using suic-econ-table2.txt,  ///
	cells("b(fmt(%4.2f)) ci(par( ( , ) ))") collabels(Est. 95%CI) nostar not ///
	nolines nonum delimiter(";") replace
	
log close
exit	
